#include "qresourcelistwidget.h"

#include <QItemDelegate>
#include <QPainter>

using namespace CORELIB;

namespace CORELIB
{
    class ItemDelegate: public QItemDelegate
    {
    public :
        virtual QSize       sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
        virtual void        paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    };
}

QSize   ItemDelegate::sizeHint(const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) const
{
    return QSize(48,48);
}

void    ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(option.state & QStyle::State_Selected)
    {
        painter->fillRect(option.rect,QColor(128,128,128,80));
    }
    QItemDelegate::paint(painter,option,index);
}

QResourceListWidget::QResourceListWidget(QWidget *parent) :
    QListWidget(parent)
{
    setFrameStyle(QFrame::NoFrame);
    setViewMode(QListView::IconMode);
    setTextElideMode(Qt::ElideMiddle);
    setItemDelegate(new ItemDelegate);
    setResizeMode(QListView::Adjust);
    setDragEnabled(false);
}

void QResourceListWidget::add(const QString &name, const QIcon &icon)
{
    int index=name.lastIndexOf("/");
    QString str=name.mid(index+1);
    QListWidgetItem *item=m_nameToItem.value(name);
    if(item==NULL)
    {
        item=new QListWidgetItem(this);
        m_nameToItem.insert(name,item);
        m_itemToName.insert(item,name);
    }
    item->setText(str);
    item->setToolTip(str);
    item->setIcon(icon);
}

QSize QResourceListWidget::ContentsSize()
{
    return contentsSize();
}

QString QResourceListWidget::getName(QListWidgetItem *item)
{
    return m_itemToName.value(item,"");
}
